package com.hoopchina.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;

import android.app.Service;
import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

/**
 * 
 * @author Guo Baiyu
 *
 */
public class DeviceInfoUtils {
	
	/**
	 * get device's IMSI
	 * @param paramContext
	 * @return
	 */
    public static String getIMSI(Context paramContext) {
	return ((TelephonyManager) paramContext.getSystemService("phone"))
		.getSubscriberId();
    }

    /**
     * get device's IMEI
     * @param paramContext
     * @return
     */
    public static String getIMEI(Context paramContext) {
	return ((TelephonyManager) paramContext.getSystemService("phone")).getDeviceId();
    }

    /**
     * get device's MODEL
     * @return
     */
    public static String getMobileModel() {
    	return Build.MODEL;
    }

    /**
     * 
     * @return
     */
    public static int getSDKVersionNumber() {
    	return Build.VERSION.SDK_INT;
    }

    // need to test and add permission in AndroidManifest.xml
    public static int getWifiStatus(Context paramContext) {
	return ((WifiManager) paramContext
		.getSystemService(Service.WIFI_SERVICE)).getWifiState();
    }

    /**
     * make true current connect service is wifi
     * 
     * @param mContext
     * @return
     */
    public static boolean isWifi(Context mContext) {
	ConnectivityManager connectivityManager = (ConnectivityManager) mContext
		.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
	if (activeNetInfo != null
		&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
	    return true;
	}
	return false;
    }

    /**
     * 检查运营商网络是否可用
     * 
     * @param mContext
     * @return
     */
    public static boolean checkNet(Context mContext) {
	ConnectivityManager manager = (ConnectivityManager) mContext
		.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo netWrokInfo = manager.getActiveNetworkInfo();
	if (netWrokInfo == null || !netWrokInfo.isAvailable()) {

	    // Toast.LENGTH_LONG).show();
	    return false;
	} else if (netWrokInfo.getTypeName().equals("MOBILE")
		&& netWrokInfo.getExtraInfo().equals("cmwap")) {

	    // Toast.LENGTH_LONG).show();
	    return false;
	} else {
	    return true;
	}
    }

    /**
     * 检查是否可以下载
     * 
     * @param mContext
     * @return
     */
    public static boolean isAllowDown(Context mContext) {
	boolean isAllowDown = true;
	Properties properties = new Properties();
	try {
	    properties.load(mContext.openFileInput("isdownwifi.properties"));
	    String lan = properties.getProperty("isdownwifi");
	    if (lan.equals("true")) {
		if (DeviceInfoUtils.isWifi(mContext)) {
		    isAllowDown = true;
		} else {
		    isAllowDown = false;
//		    Toast.makeText(mContext, "WIFI 网络不可用", 3000).show();
		}
	    } else {
		isAllowDown = true;
	    }

	} catch (Exception e) {
	    e.printStackTrace();

	}
	return isAllowDown;
    }

    /**
     * 获取设备ID
     * 
     * @param mContext
     * @return
     */
    public static String getDeviceId(Context mContext) {
	Properties properties = new Properties();
	try {
	    properties.load(mContext.openFileInput("settings.properties"));
	    String deviceid = properties.getProperty("deviceid");
	    return deviceid;

	} catch (Exception e) {
	    e.printStackTrace();
	    return "";
	}
    }

    public static void setDeviceId(Context mContext) {
		String deviceId = getIMEI(mContext);
		Calendar calendar = Calendar.getInstance();// 获取当前日历对象
		long unixTime = calendar.getTimeInMillis();// 获取当前时区下日期时间对应的时间戳
		deviceId += String.valueOf(unixTime);
	
		Properties properties = new Properties();
		properties.setProperty("deviceid", deviceId);
		try {
		    properties.store(mContext.openFileOutput("settings.properties",
			    Context.MODE_WORLD_WRITEABLE), null);
		} catch (FileNotFoundException e) {
		    e.printStackTrace();
		} catch (IOException e) {
		    e.printStackTrace();
		}
    }
}
